<?php
namespace Forum\Controller;
use Think\Controller;
class ThreadController extends CommonController {
    public function index(){
    }  

    /*
     * 主题列表
     */
    public function lists(){

            /**
             * 筛选条件
             */
            //圈子
            if(I("get.fid")&&I("get.fid")!='all'){
                $map['fid']=I("get.fid");
            }
            //主题
            if(I("get.subject")){
               $map['subject']=array('like','%'.I("get.subject").'%'); 
            }
            //用户
            if(I("get.userid")){
                $map['userid']=I("get.userid");
            }
            //置顶
            if(!empty(I("get.issticktop"))){
                $map['issticktop']=I("get.issticktop");
            }
            //高亮推荐
            if(!empty(I("get.ishighlight"))){
                $map['ishighlight']=I("get.ishighlight");
            }
            //精华
            if(!empty(I("get.isdigest"))){
                $map['isdigest']=I("get.isdigest");
            }
            //热门
            if(!empty(I("get.ishot"))){
                $map['ishot']=I("get.ishot");
            }
            //图文
            if(!empty(I("get.isthumb"))){
                $map['isthumb']=I("get.isthumb");
            }


            $DB = M("forum_threads");

            $res = $DB->where($map)->order('ctime desc')->select();

            foreach ($res as $key => $value) {
        	   //圈子名称
        	   $res[$key]['forum_name'] = $this->get_forum_name_by_fid($value['fid']);

        	   //用户昵称
        	   $res[$key]['user'] = $this->get_user_nickname_by_userid($value['userid']);

        	   //主题举报记录
        	   $map_threads_tipoff['tid']=$res[$key]['tid'];
        	   $res[$key]['tipoff_record']=M("forum_threads_tipoff_record")->where($map_threads_tipoff)->count();
            }

            $this->assign("results", $res);

            /*
             * 圈子列表
             */
            $DB_forum = M("forum_forum");
            $res_forum = $DB_forum->select();
            $this->assign("forums", $res_forum); 

            $this->display();
 
        
    }

    /**public function lists(){

        $DB = M("forum_threads");

        $res = $DB->where($map)->order('ctime desc')->select();

        foreach ($res as $key => $value) {
            //圈子名称
            $res[$key]['forum_name'] = $this->get_forum_name_by_fid($value['fid']);

            //用户昵称
            $res[$key]['user'] = $this->get_user_nickname_by_userid($value['userid']);

            //主题举报记录
            $map_threads_tipoff['tid']=$res[$key]['tid'];
            $res[$key]['tipoff_record']=M("forum_threads_tipoff_record")->where($map_threads_tipoff)->count();
        }

        $this->assign("results", $res);

        $DB_forum = M("forum_forum");
        $res_forum = $DB_forum->select();
        $this->assign("forums", $res_forum); 

        $this->display();
        
    }**/

    /**
     * 上传
     * 
     */
    public function upload() {
        if(!empty($_FILES['file']['tmp_name'])){
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     31457280 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            //$upload->rootPath  =      'Public/'; // 设置附件上传目录
            $upload->savePath  =      'forum/'; // 设置附件上传目录
                
            // 上传文件 
            $info   =   $upload->uploadOne($_FILES['file']);
            //插入到 forum_images 表 得到 id;
            $DB = M("forum_images");
            $data['url']="/".$upload->rootPath.$info['savepath'].$info['savename'];
            $id = $DB->add($data);
            
            //输出 id,前端response接收
            echo $id;
            //$this->display();
         }         
    }

    /*
     * 添加主题
     */
    public function add() {

        if(IS_POST){
            $data['subject']=I("post.subject");
            $data['fid']=I("post.fid");
            $data['isspecial']=I("post.isspecial");
            $data['invisible']=I("post.invisible");
            $data['issticktop']=I("post.issticktop");
            $data['isdigest']=I("post.isdigest");
            $data['ishot']=I("post.ishot");
            $data['ishighlight']=I("post.ishighlight");

            if(I("post.images")){
                $data['isthumb']=1;
            }
            
            $data['userid'] = I("post.userid");
            $data['lastpostuserid'] = I("post.userid");

            $data['lastposttime'] = I("server.REQUEST_TIME");
            $data['ctime'] = I("server.REQUEST_TIME");

            $DB = M("forum_threads");
            $tid = $DB->add($data);

            if($tid){

                //保存主贴内容
                $data_post['fid'] = I("post.fid");
                $data_post['tid'] = $tid;

                $data_post['userid']=I("post.userid");
                $data_post['subject']=I("post.subject");
                $data_post['message'] = I("post.message");

                $data_post['reply_pid']=0;
                $data_post['isfirst'] = 1;
                $data_post['invisible']=0;

                //图片id
                $images=I("post.images");
                $data_post['images']=substr($images,0,strlen($images)-1);

                $data_post['ctime'] = I("server.REQUEST_TIME");

                $pid = M("forum_posts")->add($data_post);

                $this->success("添加成功",U('modify',array('tid'=>$tid)));
            }else{
                $this->error("操作失败",U('lists'));
            }

        } else {

            /*
             * 加载圈子
             */
            $DB_forum = M("forum_forum");
            $res_forum = $DB_forum->select();
            $this->assign("forums", $res_forum);           
            $this->display();  
        }           
    }

    /*
     * 主题回帖
     */
    public function replies() {

        if(IS_POST){          
            $data['fid']=I("post.fid");
            $data['tid']=I("post.tid");
            $data['userid']=I("post.userid");
            $data['subject']=I("post.subject");
            $data['message']=I("post.message");
            //图片id
            $images=I("post.images");
            $data['images']=substr($images,0,strlen($images)-1);

            $data['isfirst']=0;
            $data['invisible']=0;
            $data['ctime'] = I("server.REQUEST_TIME");

            $DB = M("forum_posts");
            $id = $DB->add($data);

            if($id){
                $this->do_thread_view(I("post.userid"),I("post.tid"));//阅读
                $this->do_thread_comment(I("post.userid"),I("post.tid"));//回帖
                $this->success("添加成功",U('lists'));
            }else{
                $this->error("操作失败",U('lists'));
            }
        }          
    }

    /*
     * 主题修改
     * 
     * 
     */
    public function modify() {
        
        if(IS_POST){
            $tid = I("post.tid");

            //主题
            $thread = M("forum_threads");
            $thread->tid = $tid;
            $thread->subject=I("post.subject");

            $thread->isspecial=I("post.isspecial");
            $thread->invisible=I("post.invisible");

            $thread->issticktop=I("post.issticktop")? 1 : 0;
            $thread->isdigest=I("post.isdigest")? 1 : 0;
            $thread->ishot=I("post.ishot")? 1 : 0;
            $thread->ishighlight=I("post.ishighlight")? 1 : 0;

            $thread->utime= I("server.REQUEST_TIME");

            //帖子
            $post = M("forum_posts");
            $post->message = I("post.message");

            //图片id
            $images=I("post.images");
            $post->images = substr($images,0,strlen($images)-1);

            $map_posts['isfirst']=1;
            $map_posts['invisible']=0;
            $map_posts['tid']=$tid;
            
            if(($thread->save()) !== false && ($post->where($map_posts)->save()) !== false){
                $this->success("修改成功",U('lists'));
            }else{
                $this->error("操作失败");
            }
            
        }else{
        	$tid = I("get.tid");
        
            $DB = M("forum_threads");

            $res = $DB->find($tid);

            //圈子名称
            $res['forum_name'] = $this->get_forum_name_by_fid($res['fid']);

            //发帖用户昵称
            $res['user'] = $this->get_user_nickname_by_userid($res['userid']);

            //最后回帖用户昵称
            $res['lastpostuser'] = $this->get_user_nickname_by_userid($res['lastpostuserid']);

        	//取出主贴
        	$map_posts['isfirst']=1;
        	$map_posts['invisible']=0;
        	$map_posts['fid']=$res['fid'];
        	$map_posts['tid']=$res['tid'];
        	$res['message']=M("forum_posts")->where($map_posts)->getField('message');

            //图片
            $img_arr=M("forum_posts")->where($map_posts)->getField('images');
            //$res['image'] = $this->get_post_images($img_arr);
            if($img_arr){
                $res['image'] = $img_arr.',';
            } else {
                $res['image'] = $img_arr;
            }
            

            //主题举报记录
            $map_threads_tipoff['tid']=$tid;
            $res['tipoff_record']=M("forum_threads_tipoff_record")->where($map_threads_tipoff)->count();

            $this->assign("images", $res['image']);

            $this->assign("result", $res);
        	$this->display();  
        }
        
    }


    /*
     * 获取主帖的图片
     */
    public function thread_images_list($tid) {

        $map_posts['tid']=$tid;
        $map_posts['isfirst']=1;

        $img_arr=M("forum_posts")->where($map_posts)->getField('images');
        $res=$this->get_post_images($img_arr);
        foreach ($res as $key => $value) {

            //$res[$key]['host']=$_SERVER['HTTP_HOST'];
            $file = file_get_contents($_SERVER['HTTP_HOST'].$res[$key]['url']);           
            $res[$key]['size']=strlen($file);
        }
        if(!empty($res)){
            $this->ajaxReturn($res,'JSON');
        }        
    }

    /*
     * 主题回复记录列表
     */
    public function comment_record(){
        $tid = I("get.tid");

        //取出主贴id
        $map_posts['isfirst']=1;
        //$map_posts['invisible']=0;
        $map_posts['tid']=$tid;
        $first_id=M("forum_posts")->where($map_posts)->getField('pid');
        //$this->assign("pid", $first_id);

        //回复记录
        $DB = M("forum_posts");
        $map['isfirst']=0;
        $map['tid']=$tid;
            
        $res = $DB->where($map)->order('ctime desc')->select();

        foreach ($res as $key => $value) {
            //用户昵称
            $res[$key]['user'] = $this->get_user_nickname_by_userid($value['userid']);

            //楼层(按时间倒序计算)
            if(((count($res)-1)-$key)==0){
               $res[$key]['floor']="沙发";
            } elseif (((count($res)-1)-$key)==1) {
                $res[$key]['floor']="板凳";
            } else {
                $res[$key]['floor']="#"+($key+1);
            }

            //回复类型
            if($res[$key]['reply_pid']==$first_id) {
                $res[$key]['reply_type']="回帖";
            } else {
                $res[$key]['reply_type']="回楼层";             
            }

            //帖子举报记录
            $map_posts_tipoff['pid']=$res[$key]['pid'];
            $res[$key]['tipoff_record']=M("forum_posts_tipoff_record")->where($map_posts_tipoff)->count();

        }

        $this->assign("results", $res);

        //取出主题标题
        $subject = $this->get_forum_name_by_fid($tid);
        $this->assign("subject", $subject);

        $this->display();
        
    }

    /*
     * 主题阅读记录列表
     */
    public function read_record(){
        $tid = I("get.tid");

        $DB = M("forum_threads_read_record");
        $map['tid']=$tid;
            
        $res = $DB->where($map)->select();

        foreach ($res as $key => $value) {
            //用户昵称
            $res[$key]['user'] = $this->get_user_nickname_by_userid($value['userid']);

        }

        $this->assign("results", $res);

        //取出主题标题
        $subject = $this->get_forum_name_by_fid($tid);
        $this->assign("subject", $subject);

        $this->display();
        
    }

    /*
     * 主题点赞记录列表
     */
    public function comment_like_record(){
        $tid = I("get.tid");

        $DB = M("forum_threads_comment_like_record");
        $map['tid']=$tid;
            
        $res = $DB->where($map)->select();

        foreach ($res as $key => $value) {
            //用户昵称
            $res[$key]['user'] = $this->get_user_nickname_by_userid($value['userid']);

        }

        $this->assign("results", $res);

        //取出主题标题
        $subject = $this->get_forum_name_by_fid($tid);
        $this->assign("subject", $subject);

        $this->display();
        
    }

    /*
     * 主题收藏记录列表
     */
    public function collection_record(){
        $tid = I("get.tid");

        $DB = M("forum_threads_collection_record");
        $map['tid']=$tid;
            
        $res = $DB->where($map)->select();

        foreach ($res as $key => $value) {
            //用户昵称
            $res[$key]['user'] = $this->get_user_nickname_by_userid($value['userid']);

        }

        $this->assign("results", $res);

        //取出主题标题
        $subject = $this->get_forum_name_by_fid($tid);
        $this->assign("subject", $subject);

        $this->display();
        
    }

    /*
     * 主题分享记录列表
     */
    public function share_record(){
        $tid = I("get.tid");

        $DB = M("forum_threads_share_record");
        $map['tid']=$tid;
            
        $res = $DB->where($map)->select();

        foreach ($res as $key => $value) {
            //用户昵称
            $res[$key]['user'] = $this->get_user_nickname_by_userid($value['userid']);

        }

        $this->assign("results", $res);

        //取出主题标题
        $subject = $this->get_forum_name_by_fid($tid);
        $this->assign("subject", $subject);

        $this->display();
        
    }

    /*
     * 主题举报记录列表
     */
    public function tipoff_record(){
        $tid = I("get.tid");

        $DB = M("forum_threads_tipoff_record");
        $map['tid']=$tid;
            
        $res = $DB->where($map)->select();

        foreach ($res as $key => $value) {
            //用户昵称
            $res[$key]['user'] = $this->get_user_nickname_by_userid($value['userid']);

        }

        $this->assign("results", $res);

        //取出主题标题
        $subject = $this->get_forum_name_by_fid($tid);
        $this->assign("subject", $subject);

        $this->display();
        
    }

    /**
     * 主题举报处理结果
     */
    public function tipoff_update(){

        if(IS_AJAX){
            $DB = M('forum_threads_tipoff_record');
            $id = I('post.id');
            $map['id'] = $id;
            $result = I('post.result');
            if($result == 1){
                $data['result'] = 0;
            }else {
                $data['result'] = 1;
            }

            $result = $DB->where($map)->save($data);

            if($result){
                echo json_encode(1);
            }else{
                echo json_encode(0);
            }
        }
    }

    /**
     * 主题可见状态更新
     * 1隐藏 0可见
     */
    public function threads_invisible_update(){

        if(IS_AJAX){
            $DB = M('forum_threads');
            $map['tid'] = I('post.tid');
            $invisible = I('post.invisible');
            if($invisible == 1){
                $data['invisible'] = 0;
            }else {
                $data['invisible'] = 1;
            }

            $result = $DB->where($map)->save($data);

            if($result){
                echo json_encode(1);
            }else{
                echo json_encode(0);
            }
        }
    }
}